package com.wpml.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Date 2021-07-27
 * @Author wangpei
 **/
public class CompletableFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> m("f_1"));
//        future.thenRun(() -> System.out.println(1));
//        future.thenRunAsync(() -> System.out.println(2));
//        future.thenRun(() -> System.out.println(3));


        /*CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> 1).whenComplete((r, e) -> System.out.println(r + " one"));

        CompletableFuture<Integer> integerCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("supplyAsync 完成");
            return 2;
        }).thenApply(r -> {
            System.out.println(r + " thenApply");
            return r + 3;
        });

        System.out.println("ret " + integerCompletableFuture.get());
        System.out.println("ret " + integerCompletableFuture1.get());*/

        CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Hello 完成");
            return "Hello";
        }).thenApplyAsync(s -> {
            System.out.println(s + " World 开始");
            return s + " World";
        }).thenAccept(System.out::println);


        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("main end...");
    }



    public static void m(String from) {
        System.out.println(from + " ... start ...");

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(from + " ... end ...");
    }
}
